package com.example.service.impl;

import java.lang.reflect.Method;
import java.math.BigDecimal;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.example.dao.FactorOptionRepository;
import com.example.dao.FactorRepository;
import com.example.dao.InsuranceFormRepository;
import com.example.dao.InsuranceRepository;
import com.example.dao.PolicyFactorRepository;
import com.example.dao.ProductRepository;
import com.example.dao.SubInsuranceDetailRepository;
import com.example.dao.SysUserRepository;
import com.example.dao.UserDao;
import com.example.entity.Factor;
import com.example.entity.FactorOption;
import com.example.entity.InsuranceForm;
import com.example.entity.PolicyFactor;
import com.example.entity.SubInsuranceDetail;
import com.example.service.FactorHandleService;
import com.example.service.ProductCenterService;

@Service
public class FactorHandleServiceImpl implements FactorHandleService,ApplicationContextAware {
	@Autowired
	private UserDao userDao;
	@Autowired
	private ProductRepository productDao;
	@Autowired
	private FactorRepository factorDao;
	@Autowired
	private FactorOptionRepository factorOptionDao;
	@Autowired
	ProductCenterService productCenterService;
	@Autowired
	InsuranceRepository insuranceDao;
	@Autowired
	InsuranceFormRepository insuranceFormDao;
	@Autowired
	SubInsuranceDetailRepository subInsuranceDetail;
	@Autowired
	PolicyFactorRepository policyFactorDao;
	@Autowired
	SysUserRepository sysUserDao;

	@Override
	public BigDecimal handleFactor(PolicyFactor policyFactor, SubInsuranceDetail subInsuranceDetail, InsuranceForm form) throws Exception{
		
		long factorId = policyFactor.getFactorId();
		Factor factor=factorDao.findById(factorId);
		String methodName=factor.getValue();
		Class<?> clazz =FactorHandleServiceImpl.class;  
		Class<?> policyFactorClazz =PolicyFactor.class;  
		Class<?> subInsuranceDetailClazz =SubInsuranceDetail.class;  
		Class<?> insuranceFormClazz =InsuranceForm.class;  
		Method method=clazz.getMethod(methodName,policyFactorClazz,subInsuranceDetailClazz, insuranceFormClazz);
		FactorHandleServiceImpl one=applicationContext.getBean(FactorHandleServiceImpl.class);
		return (BigDecimal) method.invoke(one,policyFactor,subInsuranceDetail, form);
		
	}
	
	public BigDecimal limit(PolicyFactor policyFactor,SubInsuranceDetail subInsuranceDetail,InsuranceForm form){
		BigDecimal ratio=new BigDecimal(0.1);//这个值必须小于1
		BigDecimal upperRatio=BigDecimal.ONE.add(ratio);
		BigDecimal lowerRatio=BigDecimal.ONE.subtract(ratio);
		BigDecimal result=BigDecimal.ONE;
		for(PolicyFactor one:subInsuranceDetail.getPolicyFactors()){
			Factor factor = factorDao.findById(one.getFactorId());
			if(factor.getFactorDataType().equals("3")||factor.getFeeOrfacotr().equals("1")){
				continue;
			}else if(factor.getFactorDataType().equals("1")){
				String value = factor.getValue();
				result=result.multiply(new BigDecimal(value));
			}else if(factor.getFactorDataType().equals("2")){
				FactorOption factorOption = factorOptionDao.findById(one.getFactorOptionId());
				String value=factorOption.getValue();
				result=result.multiply(new BigDecimal(value));
			}else if(factor.getFactorDataType().equals("4")){
				String value=one.getFactorOptionValue();
				result=result.multiply(new BigDecimal(value));
			}
		}
		if(result.compareTo(upperRatio)>0){
			return upperRatio.divide(result,4,BigDecimal.ROUND_HALF_UP);
		}else if(result.compareTo(lowerRatio)<0){
			return lowerRatio.divide(result,4,BigDecimal.ROUND_HALF_UP);
		}else{
			return BigDecimal.ONE;
		}
	}
	
	public BigDecimal dataFactor(PolicyFactor policyFactor,SubInsuranceDetail subInsuranceDetail,InsuranceForm form){
		BigDecimal now=BigDecimal.ONE;
		BigDecimal start =new BigDecimal( subInsuranceDetail.getBackup1());
		BigDecimal substract=start.subtract(now);
		
		return substract.compareTo(BigDecimal.ONE)>0?new BigDecimal("1"):new BigDecimal("1.1");
	}
	
	private ApplicationContext applicationContext;
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.applicationContext=applicationContext;
	}

}
